package com.example.player.presentation.fragment;

import android.content.ClipData;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.example.player.R;
import com.example.player.domain.entity.Song;

/**
 * @author 朱文权
 * 本类是歌曲扩展界面
 */
public class ItemExtend extends DialogFragment {
    /**
     * 窗口定位锚点
     */
    private View anchor;
    /**
     * 当前视图
     */
    private View view;
    /**
     * 详细信息
     * */
    private TextView information;
    /**
     * 删除歌曲
     * */
    private TextView delete;
    /**
     * 歌曲
     * */
    private Song song;

    public ItemExtend(View anchor,Song song) {
        super();
        this.anchor = anchor;
        this.song=song;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.item_extend, container, false);
        information = view.findViewById(R.id.tv_information);
        delete = view.findViewById(R.id.tv_delete);
        information.setOnClickListener(this::onInfoClick);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        // 设置窗口宽高（屏幕宽度的80%，高度的50%）
        int width=(int)(getResources().getDisplayMetrics().widthPixels * 0.5);
        int height=(int)(getResources().getDisplayMetrics().heightPixels*0.15);
        Window window= getDialog().getWindow();
        window.setDimAmount(0f);
        if(window!=null){
            window.setLayout(width,height);
            WindowManager.LayoutParams params = window.getAttributes();
            int[] location=new int[2];
            anchor.getLocationOnScreen(location);
            params.gravity= Gravity.TOP|Gravity.START;
            params.x=location[0]+100;
            params.y=(int)(location[1]/1.3);
            window.setAttributes(params);
        }
    }
    private void onInfoClick(View view){
        new SongInfoDialog(song).show(getActivity().getSupportFragmentManager(), "song_info_dialog");
    }
}
